package cn.jekun.web.agent.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.omg.CORBA.PRIVATE_MEMBER;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import cn.jekun.component.base.bean.ErrorMsg;
import cn.jekun.component.base.bean.Page;
import cn.jekun.component.base.exceptions.BusinessException;
import cn.jekun.component.base.validator.annotation.D;
import cn.jekun.component.base.validator.annotation.NotBlank;
import cn.jekun.component.core.Constants;
import cn.jekun.component.core.bean.Agent;
import cn.jekun.component.core.bean.gen.RsAgent;
import cn.jekun.component.core.bean.gen.TsAccount;
import cn.jekun.component.core.bean.gen.TsOperationCode;
import cn.jekun.component.core.bean.gen.RsAgent.AnnoAgentId;
import cn.jekun.component.core.service.AccountPayService;
import cn.jekun.web.agent.service.AccountService;

/**
 * 账户信息查询
 * 
 * @author Mr.Dong
 *
 */
@SuppressWarnings("serial")
public class AccountAction extends AgentBaseAction {
	//private static final String AUTH_SHOP_MY_SECURITY="SHOP_MY_SECURITY";//安全设置
	private List<TsOperationCode> resultOperCodeList = new ArrayList<TsOperationCode>();
	private List<Agent> resultQueryDateList = new ArrayList<Agent>();
	private Agent rsAgent;
	
	public Agent getRsAgent() {
		return rsAgent;
	}
	public void setRsAgent(Agent rsAgent) {
		this.rsAgent = rsAgent;
	}
	public List<Agent> getResultQueryDateList() {
		return resultQueryDateList;
	}
	public void setResultQueryDateList(List<Agent> resultQueryDateList) {
		this.resultQueryDateList = resultQueryDateList;
	}
	private String queryDate; //日期查询条件 
	private String operType;  //账户流水类型
	private String beginMoney; //开始金额
	private String endMoney;   //结束金额
	private String beginDate;  //开始日期
	private String endDate;    //结束日期
	private String search_date;
	private String orderId;
	public String getSearch_date() {
		return search_date;
	}
	public void setSearch_date(String search_date) {
		this.search_date = search_date;
	}
	public String getBeginDate() {
		return beginDate;
	}
	public void setBeginDate(String beginDate) {
		this.beginDate = beginDate;
	}
	public String getEndDate() {
		return endDate;
	}
	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}
	
	public String getOrderId() {
		return orderId;
	}
	public void setOrderId(String orderId) {
		this.orderId = orderId;
	}
	private String keyRange;   //关键词
	private String keyword_s;  //关键词查询条件
	public String getQueryDate() {
		return queryDate;
	}
	public void setQueryDate(String queryDate) {
		this.queryDate = queryDate;
	}
	public String getOperType() {
		return operType;
	}
	public void setOperType(String operType) {
		this.operType = operType;
	}
	public String getBeginMoney() {
		return beginMoney;
	}
	public void setBeginMoney(String beginMoney) {
		this.beginMoney = beginMoney;
	}
	public String getEndMoney() {
		return endMoney;
	}
	public void setEndMoney(String endMoney) {
		this.endMoney = endMoney;
	}
	public String getKeyRange() {
		return keyRange;
	}
	public void setKeyRange(String keyRange) {
		this.keyRange = keyRange;
	}
	public String getKeyword_s() {
		return keyword_s;
	}
	public void setKeyword_s(String keyword_s) {
		this.keyword_s = keyword_s;
	}
	public List<TsOperationCode> getResultOperCodeList() {
		return resultOperCodeList;
	}
	public void setResultOperCodeList(List<TsOperationCode> resultOperCodeList) {
		this.resultOperCodeList = resultOperCodeList;
	}

	public AccountService getAccountService() {
		return accountService;
	}
	public void setAccountService(AccountService accountService) {
		this.accountService = accountService;
	}
	@Resource(name=AccountService.BEAN_NAME)
	AccountService accountService;

	@Resource(name=AccountPayService.BEAN_NAME)
	AccountPayService accountPayService;

	
	
	@SuppressWarnings("unchecked")
	public String getAccountDetails(){
        String agentId = this.getSessionUser().getAgentId();		
		JSONObject jsonObject = new JSONObject();
		JSONArray jsonArray = new JSONArray();
		Map<String,Object> hashMap ;
//		//判断是否有操作权限
//		this.checkRight(AUTH_SHOP_MY_SECURITY);
		try {
			this.resultList=accountPayService.getAccountDetails(queryDate, operType, beginMoney, endMoney,keyRange,beginDate,endDate,keyword_s,agentId,page,orderId);
			for(int i=0,length = resultList.size();i<length;i++){
				hashMap = new HashMap<String, Object>();
				Agent agent = (Agent) resultList.get(i);
				hashMap.put("createTime",agent.getCreateTime()==null?"":this.DateToDString(agent.getCreateTime()));
				hashMap.put("accountJourId", agent.getAccountJourId());
				hashMap.put("orderId", agent.getOrderId());
				hashMap.put("operName",agent.getOperName());
				hashMap.put("amount", Double.parseDouble(agent.getAmount()));
				hashMap.put("operCode",agent.getOperCode()==null?"":agent.getOperCode());
				jsonArray.add(hashMap);
			}
			jsonObject.put("resultList",jsonArray);
		    jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_OK);
			jsonObject.put(ErrorMsg.KEY_MESSAGE,"调用成功");
            jsonObject.put(Constants.Page.PAGE_NUMBER,page.getPageNumber());
			jsonObject.put(Constants.Page.PAGE_SIZE,page.getPageSize());
			jsonObject.put(Constants.Page.TOTAL_COUNT,page.getTotalCount());
			jsonObject.put(Constants.Page.TOTAL_PAGE, page.getPages());
			//查找是否存在
			TsAccount balance = accountPayService.getAccountInfo(agentId,Constants.TS_ACCOUNT_OWNER_TYPE.AGENT);
			java.text.NumberFormat nf = java.text.NumberFormat.getInstance();   
			nf.setGroupingUsed(false); 
			jsonObject.element("balance",balance==null?"0":nf.format(balance.getBalance()));
		}catch (BusinessException e) {
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
		    jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
		}
		return this.writeJson(jsonObject);
	}
	/**
	 * 获取提现流水详情
	 * @return
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public String getAccountDetailsById(){
		 JSONObject jsonObject = new JSONObject();	
		 Map map = new HashMap();
		 String accountJourId = request.getParameter("accountJourId");
		 try{
			 cn.jekun.web.agent.bean.Agent agent = accountService.getAccountDetailsById(accountJourId);
			 if(agent!=null){
				jsonObject.put("applystautusId",agent.getApplyStatusId()==null?"":agent.getApplyStatusId());
				jsonObject.put("applystautusname",agent.getApplyStatusName()==null?"":agent.getApplyStatusName());
				jsonObject.put("settleResultId",agent.getSettleResultId()==null?"":agent.getSettleResultId());
				jsonObject.put("settleresult",agent.getSettleResult()==null?"":agent.getSettleResult());
				jsonObject.put("accountJourId",agent.getAccountJourId()==null?"":agent.getAccountJourId());
				jsonObject.put("accountId",agent.getAccountId()==null?"":agent.getAccountId());
				jsonObject.put("applyCommitTime",agent.getApplycommitTime()==null?"":agent.getApplycommitTime());
				jsonObject.put("amount",agent.getAmount()==null?"":agent.getAmount());
				jsonObject.put("fee",agent.getFee()==null?"":agent.getFee());
				jsonObject.put("applyTypeName",agent.getApplyTypeName()==null?"":agent.getApplyTypeName());
				jsonObject.put("reqBankTime",agent.getReqBankTime()==null?"":agent.getReqBankTime());
				jsonObject.put("operationName",agent.getOperationName()==null?"":agent.getOperationName());
				jsonObject.put("finishBankTime",agent.getFinishBankTime()==null?"":agent.getFinishBankTime());
			}
			jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_OK);
			jsonObject.put(ErrorMsg.KEY_MESSAGE,"调用成功");
		
		}catch (BusinessException e) {
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
		    jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
		}
		return this.writeJson(jsonObject);
	}
	
	 /**
     * 获取类型
     * @return
     */
	public String getAccountType(){
		JSONArray jsonArray = null;
		JSONObject jsonObject = null;
		Map<String, Object> hashMap = null;
		try{
			jsonObject = new JSONObject();
			jsonArray = new JSONArray();
			hashMap = new HashMap<String, Object>();
			resultOperCodeList = this.accountService.getOperTypes();
			for(int i=0,size = resultOperCodeList.size();i<size;i++){
				hashMap.put("operationCode", resultOperCodeList.get(i).getOperationCode());
				hashMap.put("operation", resultOperCodeList.get(i).getOperation());
				jsonArray.add(hashMap);
			}
			jsonObject.put("resultOperCodeList",jsonArray);
			jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_OK);
			jsonObject.put(ErrorMsg.KEY_MESSAGE,"调用成功");
			
		}catch (BusinessException e) {
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
		    jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
		}
	    return this.writeJson(jsonObject);
	}
	
	  int red = 0;
	  public enum ColorSelect {
	          
	    }

}
